package cn.sinobest.pe.factory;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import cn.com.hnisi.framework.util.Configuration;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBFactory {
	private final static Logger LOG = LoggerFactory.getLogger(DBFactory.class);

	private static ComboPooledDataSource cpds;

	public static DataSource getDataSource(Configuration config){
		if(cpds == null){
			cpds = new ComboPooledDataSource();
			try {
				cpds.setDriverClass(config.getValue("db_driverClassName"));
				cpds.setJdbcUrl(config.getValue("db_url"));
				cpds.setUser(config.getValue("db_username"));
				cpds.setPassword(config.getValue("db_password"));
				
				cpds.setTestConnectionOnCheckin(true);
				cpds.setIdleConnectionTestPeriod(3600);
				cpds.setMaxStatements(0);
				cpds.setMaxIdleTime(60);
			} catch (PropertyVetoException e) {
				LOG.error("获取数据库连接失败：", e);
				return null;
			}
		}
		return cpds;
	}

}
